package cn.stylefeng.guns.modular.weixin.utils;

import java.io.*;

public class AppendFile
{
    public static void method1(final String file, final String conent) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
            out.write(conent);
        }
        catch (Exception e) {
            e.printStackTrace();
            try {
                if (out != null) {
                    out.close();
                }
            }
            catch (IOException e2) {
                e2.printStackTrace();
            }
        }
        finally {
            try {
                if (out != null) {
                    out.close();
                }
            }
            catch (IOException e3) {
                e3.printStackTrace();
            }
        }
    }
    
    public static void method2(final String fileName, final String content) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(fileName, true);
            writer.write(content);
        }
        catch (IOException e) {
            e.printStackTrace();
            try {
                if (writer != null) {
                    writer.close();
                }
            }
            catch (IOException e2) {
                e2.printStackTrace();
            }
        }
        finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            }
            catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    }
    
    public static void method3(final String fileName, final String content) {
        RandomAccessFile randomFile = null;
        try {
            randomFile = new RandomAccessFile(fileName, "rw");
            final long fileLength = randomFile.length();
            randomFile.seek(fileLength);
            randomFile.writeBytes(content);
        }
        catch (IOException e) {
            e.printStackTrace();
            if (randomFile != null) {
                try {
                    randomFile.close();
                }
                catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
        finally {
            if (randomFile != null) {
                try {
                    randomFile.close();
                }
                catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
    
    public static void main(final String[] args) {
        try {
            final File file = new File("d://text.txt");
            if (file.createNewFile()) {
                System.out.println("Create file successed");
            }
            method1("d://text.txt", "123");
            method2("d://text.txt", "123");
            method3("d://text.txt", "123");
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
